package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class ChatMemberPeer extends BasePeer{
	public static final String tableName = "ChatMember";
	public static final String CHATROOMID = "chatroomId";
	public static final String USERID = "userId";
	public static final String NICENAME = "nicename";
	public static final String TYPE = "type";
	public static final String[] columnNames=new String[]{"chatroomId","userId","nicename","type"};
	public static ChatMember getChatMemberByPKs(Object chatroomId,Object userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(ChatMemberPeer.tableName,ChatMemberPeer.columnNames," TRUE  AND `chatroomId`=?  AND `userId`=? ",new Object[]{chatroomId,userId,},0, 1,conn);
			ChatMember chatMember = new ChatMember();
		fill(rs,chatMember);
		return chatMember;
	}
	public static ChatMember getChatMemberReferenceColumnsOnlyByPKs(Object chatroomId,Object userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(ChatMemberPeer.tableName,new String[]{"chatroomId","userId",}," TRUE AND `chatroomId`=?AND `userId`=?",new Object[]{chatroomId,userId,}, 0,  1,conn);
			ChatMember chatMember = new ChatMember();
		fill(rs,chatMember);
		return chatMember;
	}
	public static ChatMember getChatMemberByChatroomId(long chatroomId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(ChatMemberPeer.tableName,ChatMemberPeer.columnNames,"`chatroomId`=?",new Object[]{chatroomId},0, 1,conn);
			ChatMember chatMember = new ChatMember();
		fill(rs,chatMember);
		return chatMember;
	}
	public static ChatMember getChatMemberByUserId(long userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(ChatMemberPeer.tableName,ChatMemberPeer.columnNames,"`userId`=?",new Object[]{userId},0, 1,conn);
			ChatMember chatMember = new ChatMember();
		fill(rs,chatMember);
		return chatMember;
	}
	public static void fill(ResultSet rs, ChatMember chatMember) throws SQLException {
		if(rs.next()){
			try{chatMember.setChatroomId(rs.getLong("chatroomId"));}catch(Exception ex){}
			try{chatMember.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{chatMember.setNicename(rs.getString("nicename"));}catch(Exception ex){}
			try{chatMember.setType(rs.getLong("type"));}catch(Exception ex){}
			chatMember.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<ChatMember> chatMemberCollection) throws SQLException {
		while(rs.next()){
			ChatMember chatMember=new ChatMember();
			try{chatMember.setChatroomId(rs.getLong("chatroomId"));}catch(Exception ex){}
			try{chatMember.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{chatMember.setNicename(rs.getString("nicename"));}catch(Exception ex){}
			try{chatMember.setType(rs.getLong("type"));}catch(Exception ex){}
			chatMember.resetModified();
			chatMemberCollection.add(chatMember);
		}
	}
	public static boolean update(ChatMember chatMember, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < ChatMemberPeer.columnNames.length; i++) {
			if (((ChatMemberBase)chatMember).modified[i]) {
				columns.add(ChatMemberPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `ChatMember` SET ");
		sql.append("WHERE TRUE AND `chatroomId` = ? AND `userId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,chatMember.get(column));
		}
		pstmt.setObject(columns.size()+1+0,chatMember.get("chatroomId"));
		pstmt.setObject(columns.size()+1+1,chatMember.get("userId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(ChatMember chatMember, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (chatMember.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `ChatMember` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (chatMember.get(column) != null) {
				pstmt.setObject(i + 1 + j, chatMember.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<ChatMember> getChatMemberCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<ChatMember> collection=new Vector<ChatMember>();
		fill(rs,collection);
		return collection;
	}
}
